package com.gl.service;

import com.gl.dao.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;

/**
 * <p>Description:service抽象类</p>
 *
 * @author gaol
 * @version 1.0
 * @date 2017/5/17
 */
public abstract class BaseService<E, R extends BaseRepository> {


    @Autowired
    protected R repository;


    public boolean exists(Long id) {
        return repository.exists(id);
    }

    public List<E> add(List<E> items) {
        return repository.save(items);
    }

    public E add(E e) {
        return (E) repository.save(e);
    }

    public void delete(E e) {
        repository.delete(e);
    }

    public void delete(Long id) {
        repository.delete(id);
    }

    public void delete(List<E> items) {
        repository.deleteInBatch(items);
    }

    public E update(E e) {
        return (E) repository.save(e);
    }

    public E findById(Long id) {
        return (E) repository.findOne(id);
    }

    public List<E> findAll() {
        return repository.findAll();
    }

    public List<E> findAll(Sort ars) {
        return repository.findAll(ars);
    }

    public List<E> findAll(Iterable<Long> var1) {
        return repository.findAll(var1);
    }


    public Page<E> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    public Page<E> findAllCondition(Pageable pageable) {
        return repository.findAll(pageable);
    }


    public List<E> findAllCondition(Specification<E> var1, Sort var2) {
        return repository.findAll(var1, var2);
    }

    public Page<E> findAllCondition(Specification<E> var1, Pageable pageable) {
        return repository.findAll(var1, pageable);
    }

    public Long findCount(Specification<E> var1) {
        return repository.count(var1);
    }

}
